package com.supreme.securityserver.security;

import com.alibaba.fastjson.JSON;
import com.supreme.commonboot.utils.AjaxResp;
import com.supreme.securityserver.utils.TokenUtils;
import io.jsonwebtoken.Claims;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component(value = "logoutSuccessHandler")
public class LogoutSuccessHandlerImpl implements LogoutSuccessHandler {

    @Resource
    private AuthTokenService authTokenService;

    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        String token = authTokenService.getToken(request);

        /*if (StringUtils.isNotBlank(token)) {
            Claims claims = TokenUtils.parseToken(token);
            String subject = claims.getSubject();
        }*/

        response.setStatus(200);
        response.setContentType("application/json");
        response.setCharacterEncoding("utf-8");
        response.getWriter().print(JSON.toJSONString(AjaxResp.instance("退出成功")));
    }
}
